﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Linq;
using System.Data;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TPFBVK03 : UIVKPOS
    {
        #region ==IsFB===========
        public static readonly DependencyProperty IsFBProperty = DependencyProperty.Register("IsFB",
            typeof(bool), typeof(TPFBVK03),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsFBChanged));
        public bool IsFB
        {
            get
            {
                return (bool)GetValue(IsFBProperty);
            }
            set
            {
                SetValue(IsFBProperty, value);
                if (MV == null) return;
                TMV.IsFB = value;
                if (!value)
                    return;
                IsFBEnd = false;
                IsFFEnd = false;
            }
        }
        private static void OnIsFBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPFBVK03;
            if (src != null)
            {
                src.IsFB = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsGD===========
        public static readonly DependencyProperty IsGDProperty = DependencyProperty.Register("IsGD",
            typeof(bool), typeof(TPFBVK03),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsGDChanged));
        public bool IsGD
        {
            get
            {
                return (bool)GetValue(IsGDProperty);
            }
            set
            {
                SetValue(IsGDProperty, value);
            }
        }
        private static void OnIsGDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPFBVK03;
            if (src != null)
            {
                src.IsGD = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsFBStarted===========
        public static readonly DependencyProperty IsFBStartedProperty = DependencyProperty.Register("IsFBStarted",
            typeof(bool), typeof(TPFBVK03),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsFBStartedChanged));
        public bool IsFBStarted
        {
            get
            {
                return (bool)GetValue(IsFBStartedProperty);
            }
            set
            {
                SetValue(IsFBStartedProperty, value);
            }
        }
        private static void OnIsFBStartedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPFBVK03;
            if (src != null)
            {
                src.IsFBStarted = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsFBEnd===========
        public static readonly DependencyProperty IsFBEndProperty = DependencyProperty.Register("IsFBEnd",
            typeof(bool), typeof(TPFBVK03),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsFBEndChanged));
        public bool IsFBEnd
        {
            get
            {
                return (bool)GetValue(IsFBEndProperty);
            }
            set
            {
                SetValue(IsFBEndProperty, value);
                if (MV == null) return;
                TMV.IsFBEnd = value;
                if (!value)
                    return;
                IsFB = false;
                IsFFEnd = false;
            }
        }
        private static void OnIsFBEndChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPFBVK03;
            if (src != null)
            {
                src.IsFBEnd = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsFFEnd===========
        public static readonly DependencyProperty IsFFEndProperty = DependencyProperty.Register("IsFFEnd",
            typeof(bool), typeof(TPFBVK03),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsFFEndChanged));
        public bool IsFFEnd
        {
            get
            {
                return (bool)GetValue(IsFFEndProperty);
            }
            set
            {
                SetValue(IsFFEndProperty, value);
                if (MV == null) return;
                TMV.IsFFEnd = value;
                if (!value)
                    return;
                IsFB = false;
                IsFBEnd = false;


            }
        }
        private static void OnIsFFEndChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPFBVK03;
            if (src != null)
            {
                src.IsFFEnd = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsCondOnly===========
        public static readonly DependencyProperty IsCondOnlyProperty = DependencyProperty.Register("IsCondOnly",
            typeof(bool), typeof(TPFBVK03),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsCondOnlyChanged));
        public bool IsCondOnly
        {
            get
            {
                return (bool)GetValue(IsCondOnlyProperty);
            }
            set
            {
                SetValue(IsCondOnlyProperty, value);  

            }
        }
        private static void OnIsCondOnlyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPFBVK03;
            if (src != null)
            {
                src.IsCondOnly = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==P01===========
        public static readonly DependencyProperty P01Property = DependencyProperty.Register("P01",
        typeof(string), typeof(TPFBVK03),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnP01Changed));
        public string P01
        {
            get
            {
                return (string)GetValue(P01Property);
            }
            set
            {
                SetValue(P01Property, value);
                if (dwMaster == null) return;
                dwMaster["P01"] = value;
            }
        }
        private static void OnP01Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPFBVK03;
            if (src != null)
            {
                src.P01 = (string)e.NewValue;
            }
        }
        #endregion

        #region ==PAGEN===========
        public static readonly DependencyProperty PAGENProperty = DependencyProperty.Register("PAGEN",
        typeof(int), typeof(TPFBVK03),
        new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPAGENChanged));
        public int PAGEN
        {
            get
            {
                return (int)GetValue(PAGENProperty);
            }
            set
            {
                SetValue(PAGENProperty, value);
            }
        }
        private static void OnPAGENChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPFBVK03;
            if (src != null)
            {
                src.PAGEN = (int)e.NewValue;
            }
        }
        #endregion 
        public TPFBVK03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVKPOS();
            this.DataContext = MV;
            MESSAGES += "VKKNEW;VVRELOAD;VVTCHG;VKPCHK;WDCHG;";
            InitializeComponent();
            VKPSA = "AD";
            DTSTART = MV.date;
            DTEND = MV.date;
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {

                DES = "";
            }
            else
            {
                DES = "强力标准:" + Helpers.Helper.GetGB2312Value(dwMaster["STRENGTH"].ToString()) + "     捻度标准:" + Helpers.Helper.GetGB2312Value(dwMaster["TWIST"].ToString()) + "     颜色标准:" + Helpers.Helper.GetGB2312Value(dwMaster["COLORSTD"].ToString());
            }
        }

        public override void OnEdit()
        {
            base.OnEdit();
            if (vwMaster.View != null)
            {
                vwMaster.View.MoveCurrentTo(-1);
            }
        }
        public override void PRODREQ()
        {
            base.PRODREQ();
            switch (app.company)
            {
                case "YGR":

                    break;
            }
            switch (ARTST)
            {
                case "G":
                    IsFBStarted = false;
                    if (dwVKP != null)
                    {
                        if (dwVKP["VKPEXAUFLFD"].ToString() != "000000")
                        {
                            IsFBStarted = true;
                        }
                    }
                    TMV.CHKGD(dwVKP, dwVF);
                    break;
            }

        }
        public override void VKPCHK(ReflectionStudio.Core.Events.MessageInfo msg)
        {
            // base.VKPCHK(msg);
            /*
            if (vwMaster.View == null)
                return;
            vwMaster.View.MoveCurrentTo(-1);*/
            int idx = -1;
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["VKPAUFKRS"].ToString().Trim() == msg.KRS.Trim()
                        && row["VKPAUFLFD"].ToString().Trim() == msg.LFD.Trim()
                        && row["VKPAUFPOS"].ToString().Trim() == msg.POS.Trim()
                        select row;
            foreach (DataRow row in query)
            {
                idx = TBMain.Rows.IndexOf(row);
                row["VKPEXART"] = msg.ART;
                break;
            }
            if (idx < 0)
                return;
            dwData = TBMain.Rows[idx];
            SResult = TMV.QVKP110(dwData);
            DataTable tb = TBMain.Clone();
            tb.Merge(SResult.Tables[0]);
            foreach (DataRow row in tb.Rows)
            {
                dwData.ItemArray = row.ItemArray;
            }
            //vwMaster.View.MoveCurrentToPosition(idx);
        }
        public DataSet PageLoad(DataRow dwData)
        {
            DataSet Set = new DataSet();
            int rows = 20;

            SResult = TMV.QVKP11AEX(dwData, PAGEN, rows);
            Set.Merge(SResult);
            MergeData(Set, false, false);
            INFO = "已经加载：" + PAGEN * rows + "本次加载" + SResult.Tables[0].Rows.Count;
            if (SResult.Tables[0].Rows.Count == 0)
            {

                return Set; 
            } 
            PAGEN++; 
            return Set;

        }
        public   void QueryNextCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            PageLoad(dwData); 
        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            PAGEN = 0;
            TBMain.Clear();
            base.QueryCommand(sender, e);
            bool IsCOnd = false;
            dwData = TMV.YGRVKPFB(QCOND, ref IsCOnd);
            if (IsCOnd)
            {
                SResult = PageLoad(dwData); 
            }
            IsCondOnly = IsCOnd;
        }

        public void QueryTDPlanCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            dwData = TMV.MUX.TBMain.NewRow();
            dwData["ABAVKAUFTYP"] = " MU.ABAVKAUFTYP='K' AND MU.ABANEUDAT>=" + DTSTART + " AND MU.ABANEUDAT<=" + DTEND;
            SResult = TMV.QVKP111(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }
        public override void OnQCONDChg()
        {
            PAGEN = 0;
            TBMain.Clear();
            bool IsCOnd = false;
            dwData = TMV.YGRVKPFB(QCOND, ref IsCOnd);
            if (IsCOnd)
            {
                PageLoad(dwData); 
            }
            IsCondOnly = IsCOnd;
            base.OnQCONDChg();
        }

    }
}
